package com.nono.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.nono.Dao.UserDao;

@Repository
public class UserService {
	@Autowired
	UserDao userDao;
	
	public boolean addOrder(int userId, String commodityIds, String commodityCounts) {
		
		String[] commodityIdsArray = commodityIds.split(","); 
		String[] commodityCountsArray = commodityCounts.split(",");
		boolean flag = true;
		for(int i=0; i<commodityIdsArray.length; i++ ) {
			if( !userDao.addOrder( userId, Integer.parseInt( commodityIdsArray[i] ), Integer.parseInt( commodityCountsArray[i] ) ) ) {
				flag = false;
			};
		};
		
		return flag;
	}
	
	//删除购物车;
	public boolean delOrder(int userId) {
		return userDao.delOrder(userId);
	}
	
	public boolean addForm(int userId,String address,String phone,String totalPrice,String pay, String orderlistFromClient) {
		boolean flag = true;
		
		//获取购物车的数据;
		String orderlist = userDao.getOrderList( userId );
		if( orderlist.startsWith("[]") ) {
			return false;
		};
		//添加数据这些数据到订单列表orderForm列表;
		if( !userDao.addForm(userId, address, phone, totalPrice, pay, orderlistFromClient) ) {
			flag = false;
		};
		
		//删除orderlist表中用户的购物车数据;
		if(!userDao.delOrder(userId)) {
			flag = false;
		};
		return flag;
		
	}
}
